package com.ews.onlineexchange.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.collections4.MapUtils;

import com.ews.onlineexchange.model.BestTradeEvaluation;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class BestTradeEvaluationOfBulletinList extends EntityQuery<BestTradeEvaluation> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	BulletinHome3 			bulletinHome3;
	
	Map<String, Map<String, BestTradeEvaluation>> resultMap;

	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<BestTradeEvaluation> entity ) {
		
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		
		restrictions.add( cb.equal( entity.join( "trade" ).join( "bulletin" ).get( "id" ), bulletinHome3.getInstance().getId() ) );
		
		return restrictions;
		
	}

	public Map<String, Map<String, BestTradeEvaluation>> getResultMap() {
		if ( this.resultMap == null ) {
			resultMap = new HashMap<String, Map<String,BestTradeEvaluation>>();
			for ( BestTradeEvaluation _bte : getResultList() ) {
				String tradeId = _bte.getTrade().getId();
				
				Map valueLv1 = MapUtils.getMap( this.resultMap, tradeId, new HashMap<>() );
				valueLv1.put( _bte.getRequirementDefination().getId(), _bte );
				
				this.resultMap.put( tradeId, valueLv1 );
			}
		}
		return resultMap;
	}

	public BestTradeEvaluationOfBulletinList() {
		setMaxResults( null );
	}
	
	
}
